<?php namespace system\service;
/**
 * 用户管理服务
 * Class User
 * @package system\service
 */
class User {
	//登录检测
	public function isLogin() {
		if(!Session::get( 'uid' )){
			message('请登录后操作','admin/entry/login','warning');
		}
	}

	//用户登录的服务
	public function login() {
		Validate::make( [
			[ 'username', 'required', '帐号不能为空' ],
			[ 'password', 'required', '密码不能为空' ]
		] );
		$db   = new \system\model\User();
		$user = $db->where( 'username', Request::post( 'username' ) )->first();
		if ( ! $user ) {
			message( '帐号不存在', '', 'error' );
		}
		if ( $user['password'] != md5( Request::post( 'password' ) ) ) {
			message( '密码错误', '', 'error' );
		}
		$user->loginip   = Request::ip();
		$user->logintime = time();
		$user->save();
		Session::set( 'uid', $user->uid );

		return TRUE;
	}

	//退出登录
	public function quit()
	{
		if (Session::get('uid')) {
			Session::del('uid');
			return TRUE;
		}
		return false;
	}
}







